
const { menu, ccclass, property } = cc._decorator;
@ccclass
@menu("自定义插件/数组插件")
export default class MultiPlug extends cc.Component {
    @property({ type: cc.SpriteFrame, tooltip: '图片精灵' })
    sprites: cc.SpriteFrame[] = [];
    @property({ type: cc.Node, tooltip: '分页' })
    nodes: cc.Node[] = [];
    @property({ type: cc.Font, tooltip: '字体' })
    fonts: cc.Font[] = [];

    public setValue(index: number | string) {
        if (this.fonts.length > 0) {
            console.assert(index < this.fonts.length);
            this.getComponent(cc.Label).font = this.fonts[index];
        } else if (this.sprites.length > 0) {
            console.assert(index < this.sprites.length);
            this.getComponent(cc.Sprite).spriteFrame = this.sprites[index];
        } else if (this.nodes.length > 0) {
            if (typeof (index) === 'number') {
                console.assert(index < this.nodes.length);
                this.nodes.forEach((it, i) => {
                    it.active = (i === index);
                });
            } else {
                this.nodes.forEach((it: cc.Node) => {
                    it.active = (it.name === index);
                })
            }
        }
    };

    public getValue(index: number): cc.Font | cc.Sprite {
        if (this instanceof cc.Label) {
            console.assert(index < this.fonts.length);
            return this.fonts[index];
        } else if (this instanceof cc.Sprite) {
            console.assert(index < this.sprites.length);
            return this.sprites[index];
        }
        return null;
    }
}